#!/bin/bash
dbname_file=$(cat /data/shell/dbname.txt)
mysql_addr='连接地址'
dbuser='用户名'
dbpassword='密码'
backup_dir='/data.mysql'
backup_log='/data.mysql/logs/mysql_backup.log'
ymtime=$(date +%Y-%m)
dtime=$(date +%d)

wlog() {
  echo -e "$(date "+%F %T") State: $1\n" >>$backup_log
}

[ ! -d $backup_dir/$ymtime ] && mkdir -p $backup_dir/$ymtime

for dbname in $dbname_file; do
  /usr/bin/mysqldump --host=$mysql_addr --user=$dbuser --password=$dbpassword --databases $dbname --skip-lock-tables >$backup_dir/$ymtime/$dbname.sql
done

if [ $? -eq 0 ]; then
  wlog 'Backup success.'
else
  wlog 'Backup failed.'
fi
